package Fixtures.SaleInitiation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import fit.ActionFixture;

import java.sql.*;

import Common.Entities.*;

public class SaleInitiation extends ActionFixture {

	private String saleName;
	private String saleDescription;
	private String startDay;
	private String startMonth;
	private String startYear;
	private String finishDay;
	private String finishMonth;
	private String finishYear;
	private int templateID = 2;
	private ArrayList<Sale> sales;
	
	
	
	
	/**
	 * 
	 */
	public SaleInitiation() {
		saleName = "";
		saleDescription = "";
		finishDay = "";
		finishMonth = "";
		finishYear = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date current = new Date();
		String[] today = new String(sdf.format(current)).split("-");
		startDay = today[2];
		startMonth = today[1];
		startYear = today[0];
		sales = new ArrayList<Sale>();
	}
	/**
	 * @return the saleName
	 */
	public String getSaleName() {
		return saleName;
	}
	/**
	 * @param saleName the saleName to set
	 */
	public void setSaleName(String saleName) {
		this.saleName = saleName;
	}
	/**
	 * @return the saleDespription
	 */
	public String getSaleDescription() {
		return getSaleDescription();
	}
	/**
	 * @param saleDespription the saleDespription to set
	 */
	public void setSaleDescription(String saleDespription) {
		this.saleDescription = saleDespription;
	}
	/**
	 * @return the startDay
	 */
	public String getStartDay() {
		return startDay;
	}
	/**
	 * @param startDay the startDay to set
	 */
	public void setStartDay(String startDay) {
		this.startDay = startDay;
	}
	/**
	 * @return the startMonth
	 */
	public String getStartMonth() {
		return startMonth;
	}
	/**
	 * @param startMonth the startMonth to set
	 */
	public void setStartMonth(String startMonth) {
		this.startMonth = startMonth;
	}
	/**
	 * @return the startYear
	 */
	public String getStartYear() {
		return startYear;
	}
	/**
	 * @param startYear the startYear to set
	 */
	public void setStartYear(String startYear) {
		this.startYear = startYear;
	}
	/**
	 * @return the finishDay
	 */
	public String getFinishDay() {
		return finishDay;
	}
	/**
	 * @param finishDay the finishDay to set
	 */
	public void setFinishDay(String finishDay) {
		this.finishDay = finishDay;
	}
	/**
	 * @return the finishMonth
	 */
	public String getFinishMonth() {
		return finishMonth;
	}
	/**
	 * @param finishMonth the finishMonth to set
	 */
	public void setFinishMonth(String finishMonth) {
		this.finishMonth = finishMonth;
	}
	/**
	 * @return the finishYear
	 */
	public String getFinishYear() {
		return finishYear;
	}
	/**
	 * @param finishYear the finishYear to set
	 */
	public void setFinishYear(String finishYear) {
		this.finishYear = finishYear;
	}
	public boolean emptyFields(){
		if(saleName.isEmpty() || saleDescription.isEmpty() || 
				   finishDay.isEmpty() || finishMonth.isEmpty() || finishYear.isEmpty())
			return true;
		return false;
	}

	public boolean saleIsValid(){
		
		/* check if one of the fields is empty*/
		if(emptyFields())
			return false;
		/*Check if date fields are all numeric*/
			//return false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		String start = startYear + "-" + startMonth + "-" + startDay;
		String finish = finishYear + "-" + finishMonth + "-" + finishDay;
		
		/*try to convert the date fields to a java.util.Date if can't then return false*/
		try {
			Date startDate = sdf.parse(start);
			Date finishDate = sdf.parse(finish);
			Date currentDate = new Date();
			String curr = sdf.format(currentDate);
			currentDate = sdf.parse(curr);
		/*check if start date is after or is equal to finish date */
			long f = finishDate.getTime();
			long s = startDate.getTime();
			long c = currentDate.getTime();
			if (s-c < 0)
				return false;
			if (f-s <= 0)
				return false;
			sales.add(new Sale(sales.size()-1, saleName, saleDescription, startDate, finishDate, templateID));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
		return true;
			
	}
	
	
	public boolean isAdded(){
		if(!saleIsValid())
			return false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		String start = startYear + "-" + startMonth + "-" + startDay;
		String finish = finishYear + "-" + finishMonth + "-" + finishDay;
		try {
			Sale sale = new Sale(sales.size()-1, saleName, saleDescription, sdf.parse(start), sdf.parse(finish), templateID);
			Sale lastSale = sales.get(sales.size() - 1);
			if(sale.getSaleName().equals(lastSale.getSaleName()))
				return true;
			return false;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return false;
		}
	}


}
